package com.koron.css2.serviceManage.utils;

import com.koron.css2.serviceManage.mapper.DictionaryMapper;
import com.koron.css2.serviceManage.vo.DictionaryVO;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.List;
import java.util.Objects;

/**
 * 数据字典
 * @author bin
 *
 */
public class DicUtils {
	
	/**
	 * 通过code 获取数据字典集合
	 * @param factory
	 * @param code 
	 * @return 数据字典集合
	 */
	public static List<DictionaryVO> listDicByCode(SessionFactory factory, String code){
		Objects.requireNonNull(code, "数据字典code参数不能为空");
		DictionaryMapper mapper = factory.getMapper(DictionaryMapper.class);
		return mapper.findDictionaryParamName(code);
	}
	
	/**
	 * 通过数据字典code以及字典value获取数据字典的name
	 * @param factory
	 * @param code
	 * @param key
	 * @return 数据字典的value
	 */
	public static String getDicNameByCodeAndValue(SessionFactory factory, String code, String value){
		List<DictionaryVO> dics = listDicByCode(factory, code);
		DictionaryVO dic = dics.stream().filter(t -> t.getValue().equals(value)).findFirst().orElse(new DictionaryVO());
		return dic.getName();
	}

}
